/* Vue X */
import Vue from 'vue'
import Vuex from 'vuex'

import {
  getItem,
  setItem
} from '../utils/storage'

Vue.use(Vuex)

const USER_KEY = 'userInfo'

export default new Vuex.Store({
  state: {
    userInfo: getItem(USER_KEY), //当前登录用户的登录状态(token等数据)
    cachePages: ['LayoutIndex']
  },
  mutations: {
    setUser(state, val) {
      state.userInfo = val
      /* 设置 webStorage 的值 */
      //1. window.localStorage.setItem('userInfo', JSON.stringify(val))
      setItem(USER_KEY, val)
    },
    // 添加缓存页面
    addCachePage(state, pageName) {
      if (!state.cachePages.includes(pageName)) {
        state.cachePages.push(pageName)
      }
    },

    // 移除缓存页面
    removeCachePage(state, pageName) {
      let index = state.cachePages.indexOf(pageName)
      if (index != -1) {
        state.cachePages.splice(index, 1)
      }
    }
  },
  actions: {},
  modules: {}
})